package com.archive.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.archive.common.CommonEnum;
import com.archive.common.ResponseBean;
import com.archive.common.ResultUtil;
import com.archive.common.ToolsUtils;
import com.archive.dto.PageDTO;
import com.archive.entity.SysFaculty;
import com.archive.entity.SysMajor;
import com.archive.service.SysFacultyService;
import com.archive.service.SysMajorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学院表 前端控制器
 * </p>
 *
 * @author Janus
 * @since 2023-12-15
 */
@RestController
@RequestMapping("/faculty")
public class SysFacultyController {

    private static  final Logger logger= LoggerFactory.getLogger(SysFacultyController.class);

    @Autowired
    private SysFacultyService facultyService;


    @Autowired
    private SysMajorService majorService;

    /**
     * 添加
     * @param faculty
     * @return
     */
    @RequestMapping("add")
    public ResponseBean addFaculty(SysFaculty faculty, MultipartFile file){
        if(faculty==null){
            return  ResultUtil.error(CommonEnum.BAD_PARAM);
        }
        QueryWrapper<SysFaculty> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",faculty.getName());
        List<SysFaculty> checkList = facultyService.list(queryWrapper);
        if(checkList!=null&&!checkList.isEmpty()){
            return ResultUtil.error("名称已存在！");
        }

        facultyService.save(faculty);
        return  ResultUtil.success();
    }


    /**
     * 修改
     * @param faculty
     * @return
     */
    @RequestMapping("/edit")
    public ResponseBean updateById( SysFaculty faculty) {
        if (faculty == null) {
            return ResultUtil.error(CommonEnum.BAD_PARAM);
        }
        QueryWrapper<SysFaculty> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",faculty.getName()).ne("id",faculty.getId());
        List<SysFaculty> checkList = facultyService.list(queryWrapper);
        if(checkList!=null&&!checkList.isEmpty()){
            return ResultUtil.error("名称已存在！");
        }

        faculty.setUpdateTime(LocalDateTime.now());
        boolean i = facultyService.updateById(faculty);
        return ResultUtil.success() ;
    }


    /**
     * 分页列表查询
     * @param queryParam 查询参数
     * @return
     */
    @RequestMapping("/list")
    public ResponseBean getList(PageDTO pageDTO, SysFaculty queryParam ) {
        QueryWrapper<SysFaculty> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(queryParam.getName()),"name",queryParam.getName());
        queryWrapper.orderBy(true,pageDTO.getIsAsc().equals("asc"), ToolsUtils.camelToUnderline(pageDTO.getOrderByColumn()));
        IPage<SysFaculty> indexPage = new Page<>(pageDTO.getPageNum(),pageDTO.getPageSize());
        IPage<SysFaculty> listPage = facultyService.page(indexPage, queryWrapper);
        //获取分页信息
        Map pageInfo = ToolsUtils.wrapperPageInfo(listPage);
        return ResultUtil.successData(pageInfo);
    }

    /**
     * 删除
     * @param idList
     * @return
     */
    @RequestMapping("delete")
    public ResponseBean delete(@RequestParam("idList") List<Long> idList){
        if(idList==null||idList.isEmpty()){
            return ResultUtil.error("ID不合法！");
        }
        List<SysMajor> majorList = majorService.list(new QueryWrapper<SysMajor>().in("faculty_id", idList));
        if(majorList.size()>0){
            return ResultUtil.error("学院下有专业，不可删除！");
        }
        facultyService.removeByIds(idList);
        return ResultUtil.success();
    }


}
